<template>
  <div class="wrapper">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="search">
          <span>账号:</span
          ><el-input v-model="countnum" placeholder="请输入账号"></el-input>
          <el-button type="primary" @click="check">查询</el-button>
          <el-button type="primary" @click="add">新增</el-button>
        </div>
      </div>
      <div>
        <el-button @click="backtabel">返回</el-button>
      </div>
      <div class="tableform">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column type="index"> </el-table-column>
          <el-table-column prop="username" label="账号" width="300px">
          </el-table-column>
          <el-table-column prop="name" label="姓名" width="300px">
          </el-table-column>
          <el-table-column prop="right" label="权限"> </el-table-column>
          <el-table-column fixed="right" label="操作" width="300px">
            <template slot-scope="scope">
              <el-button @click="edit(scope.row)" type="text" size="small"
                >编辑</el-button
              >
              <el-button type="text" size="small" @click="deleteUser(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="ruleForm.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="支付宝" prop="airpay">
            <el-input v-model="ruleForm.airpay" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="权限"
            ><el-cascader
              v-model="rightvalue"
              :options="options"
              @change="handleChange"
            ></el-cascader
          ></el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <el-dialog title="提示" :visible.sync="dialogVisible2" width="30%">
        <el-form
          :model="userinfo"
          status-icon
          :rules="rules"
          ref="userinfo"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="userinfo.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="userinfo.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="userinfo.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="userinfo.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="支付宝" prop="airpay">
            <el-input v-model="userinfo.airpay" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="权限"
            ><el-cascader
              v-model="userinfo.right"
              :options="options"
              @change="handleChange"
            ></el-cascader
          ></el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm2('userinfo')"
              >提交</el-button
            >
            <el-button @click="cancelForm2">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      countnum: "",
      userinfo: {},
      rightvalue: "",
      options: [
        { value: "超级管理员", label: "超级管理员" },
        { value: "一级管理员", label: "一级管理员" },
        { value: "二级管理员", label: "二级管理员" },
        { value: "普通管理员", label: "普通管理员" }
      ],
      totalData: [],
      tableData: [],
      dialogVisible: false,
      dialogVisible2: false,
      ruleForm: {
        username: "",
        name: "",
        phone: "",
        airpay: "",
        pass: "",
        right: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        airpay: [{ required: true, message: "请输入支付宝", trigger: "blur" }],
        pass: [{ required: true, message: "请输入密码", trigger: "blur" }],
        right: [{ required: true }]
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    check() {
      console.log(this.countnum);
      this.tableData = [];
      this.totalData.forEach(item => {
        if (item.username == this.countnum) {
          this.tableData.push(item);
        }
      });
    },
    add() {
      this.dialogVisible = true;
      console.log("添加新成员");
    },
    edit(data) {
      console.log(data);
      this.userinfo = data;
      this.dialogVisible2 = true;
    },
    deleteUser(data) {
      var id = data.username;
      console.log(data);
      this.$confirm("您确定要删除该用户信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.totalData.forEach((item, index) => {
            if (item.username == id) {
              this.totalData.splice(index, 1);
            }
          });
          this.tabelData();
          this.dialogVisible2 = false;
          return this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.ruleForm);
          this.totalData.push(this.ruleForm);
          this.tabelData();
          this.dialogVisible = false;
          return this.$message({
            message: "添加新成员成功",
            type: "success"
          });
        } else {
          return this.$message({
            message: "请您填写准确的信息",
            type: "warning"
          });
        }
      });
    },
    cancelForm() {
      this.ruleForm = {
        username: "",
        name: "",
        phone: "",
        airpay: "",
        pass: ""
      };
      this.dialogVisible = false;
      this.$message("您已取消添加新成员");
    },
    submitForm2(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.userinfo);
          var id = this.userinfo.username;
          this.totalData.forEach(item => {
            if (item.username == id) {
              item == this.userinfo;
            }
          });
          this.tabelData();
          this.dialogVisible2 = false;
          return this.$message({
            message: "编辑成功",
            type: "success"
          });
        } else {
          return this.$message({
            message: "请您填写准确的信息",
            type: "warning"
          });
        }
      });
    },
    cancelForm2() {
      this.userinfo = {};
      this.dialogVisible2 = false;
      this.$message("您已取消编辑");
    },
    handleChange(val) {
      this.ruleForm.right = val[0];
    },
    tabelData() {
      this.tableData = this.totalData;
    },
    backtabel() {
      this.tableData = this.totalData;
      this.countnum = "";
    },
    async getTotalData() {
      await this.$http.get("../../static/json/admin.json").then(res => {
        this.totalData = res.data;
      });
      this.tabelData();
    }
  },
  created() {
    this.getTotalData();
  },
  mounted() {}
};
</script>
<style lang="scss" scoped>
.wrapper {
  width: 100%;
}
.search {
  font-size: 16px;
  .el-input {
    width: 200px;
    margin: 0 100px 0 20px;
  }
}
</style>
